package sk.stuba.fiit.team14.game.comm;

import java.util.Calendar;
import java.util.Date;

import sk.stuba.fiit.team14.game.R;
import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.caucho.hessian.client.HessianProxyFactory;

public class HessDroidClientActivity extends Activity {
	
    String webServiceUrl;
    TextView tw;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hessian_test);
        
//      webServiceUrl = "http://hessian.jaik.sk/";
//      webServiceUrl = "http://www.csemadvm.sk/hessian/;
//      webServiceUrl = "http://10.0.0.11:8080/WebApplication1/NewServlet";
        webServiceUrl = "http://vm29.ucebne.fiit.stuba.sk/server/web";
        
        tw = (TextView) findViewById(R.id.text);     
        tw.setText(webServiceUrl);
        
        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {		
			public void onClick(View v) {
			
			        HessianProxyFactory proxyFactory = new HessianProxyFactory();
					// have to set this to work correcly on android
					// http://code.google.com/p/hessdroid/issues/detail?id=9
					proxyFactory.setHessian2Reply(false);
					
			        API api;
					try {
						long ms = Calendar.getInstance().getTimeInMillis();
						api = (API) proxyFactory.create(API.class, webServiceUrl, getClassLoader());						
						api.setData( (String) DateFormat.format("yyyy/MM/dd hh:mm:ss", new Date()));
						String gotdata = api.div();
						ms = Calendar.getInstance().getTimeInMillis() - ms;				
						tw.setText("data: " + gotdata + " in " + ms + "ms");
				        Log.d("A",gotdata);
					} catch (Exception e) {
						Log.d("A",e.getMessage());
						e.printStackTrace();
						tw.setText(e.getMessage() + " " + e.getStackTrace()[0].getMethodName() + e.getStackTrace()[0].getLineNumber());
					}
				
			}
		});
                   
    }
}